import axios from 'axios'
import store from './store'
import {Loading} from 'element-ui';
// import router from '../router';

import uitls from './util';
import {API_HOST} from './api'

axios.defaults.baseURL = API_HOST;
// 设置请求头
axios.defaults.headers = {
  'Content-TYpe': 'application/json;charset=UTF-8',
  'X-Requested-With': 'XMLHttpRequest'
};
// 请求超时时间
axios.defaults.timeout = 5000;

let cancel, promiseArr = {};
let loading;
// 请求拦截器
axios.interceptors.request.use(
  config => {
    const token = store.state.token;
    config.data = JSON.stringify(config.data);
    config.headers = {
      'Content-Type': 'application/json;charset=UTF-8'
    };
    token && (config.headers.Authorization = token);
    // 发起请求时，取消掉当前正在进行的相同请求
    if (promiseArr[config.url]) {
      promiseArr[config.url]('操作取消');
      promiseArr[config.url] = cancel
    } else {
      promiseArr[config.url] = cancel
    }
    store.commit('updateLoading', true);
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
// 响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return Promise.resolve(response.data);
    } else {
      return Promise.reject(response);
    }
  },
  error => {
    if (error.response.status) {
      switch (error.response.status) {
        case 400:
          error.message = '错误请求';
          break;
        case 401:
          error.message = '未授权，请重新登录';
          uitls.toLogin();
          break;
        case 403:
          error.message = '拒绝访问';
          break;
        case 404:
          error.message = '请求错误，未找到该资源';
          break;
        case 405:
          error.message = '请求方法未允许';
          break;
        case 408:
          error.message = '请求超时';
          uitls.toLogin();
          break;
        case 500:
          error.message = '服务端出错';
          break;
        case 501:
          error.message = '网络未实现';
          break;
        case 502:
          error.message = '网络错误';
          break;
        case 503:
          error.message = '服务不可用';
          break;
        case 504:
          error.message = '网络超时';
          break;
        case 505:
          error.message = 'http版本不支持该请求';
          break;
        default:
          error.message = `链接错误${error.re.status}`
      }
      return Promise.reject(error.response);
    }
  }
);


// 封装请求方法
function formatReq(type, url, data) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: JSON.stringify(data)
    })
      .then(response => {
        store.commit('updateLoading', false); // 隐藏loading
        resolve(response);
      })
      .catch(err => {
        store.commit('updateLoading', false); // 隐藏loading
        reject(err.message);
      });
  });
}

const Http = {
  get: (url) => formatReq('get', url),
  post: (url, data) => formatReq('post', url, data),
  put: (url, data) => formatReq('put', url, data),
  patch: (url, data) => formatReq('patch', url, data),
  delete: (url, data) => formatReq('delete', url, data)
};

export default Http;
